package de.openkanban.client.datasource;

import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceImageField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.util.Page;

/**
 * Data Source for board history. Requests data over HTTP via JSON.
 */
public class BoardHistoryDS extends DataSource {

	private static BoardHistoryDS instance;

	public static BoardHistoryDS getInstance(String parentBoardId) {
		if (instance == null) {
			instance = new BoardHistoryDS("boardHistoryDS", parentBoardId);
		}
		return instance;
	}

	private BoardHistoryDS(String id, String parentBoardId) {
		setID(id);

		DataSourceTextField historyDateTime = new DataSourceTextField("historyDateTime", "Date", 100);
		DataSourceTextField namensKuerzelHistory = new DataSourceTextField("namensKuerzel", " ", 50);

		DataSourceImageField historyActionValue = new DataSourceImageField("historyActionValue", " ");
		historyActionValue.setImageURLPrefix(Page.getAppImgDir() + "history/");
		historyActionValue.setImageHeight(18);
		historyActionValue.setImageWidth(20);

		historyActionValue.setWidth(25);
		namensKuerzelHistory.setAttribute("width", 32);
		historyDateTime.setAttribute("width", 85);

		DataSourceTextField text = new DataSourceTextField("text", "Content:");

		setCacheAllData(false);
		setFields(historyDateTime, namensKuerzelHistory, historyActionValue, text);
		setDataFormat(DSDataFormat.JSON);
		setDataURL("service/board/" + parentBoardId + "/history/get");
		setClientOnly(false);
	}

	/**
	 * Override transformRequest. Here for illustration purposes only and
	 * this override implementation simply calls super.transformReRequest
	 * 
	 * @param dsRequest
	 *                the DSRequest being processed
	 * @return the transformed request
	 */
	@Override
	protected Object transformRequest(DSRequest dsRequest) {
		return super.transformRequest(dsRequest);
	}

	public static void setInstanceNull() {
		BoardHistoryDS.instance = null;
	}

}
